package LiaoTianYouxi;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    static ArrayList<Book> list2 = new ArrayList<>();
    public static Object menu() {
        StringBuilder results = new StringBuilder();
        while (true) {
            // 打印菜单
            results.append("*****欢迎来到【周诗雨的书籍城】*****\n");
            results.append("*****1.展示书籍*****\n");
            results.append("*****2.上新书籍*****\n");
            results.append("*****3.下架书籍*****\n");
            results.append("*****4.退出应用*****\n");
            results.append("请输入你想要的功能代码");
            return results.toString();
        }
    }
    public static void someMethod(int a) {
        if(a==1)
            System.out.println("【书城】》》》》》》》1.展示书籍");
        else if(a==2)
            System.out.println("【书城】》》》》》》》2.上新书籍\n");
        else if(a==3)
            System.out.println("【书城】》》》》》》》3.下架书籍\n");
        else
            System.out.println("【书城】》》》》》》》4.退出应用\n");
    }
    public static ArrayList<Book> translist(){
        return Test.list2;
    }
    public static Object getMenu1(int choice) throws IOException, ClassNotFoundException {
        File file = new File("D:\\demo.txt");
        Scanner sc = new Scanner(System.in);
        if (file.exists()) {
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            ois.close();
        }
        if (choice == 1) {
            someMethod(1);
            FileInputStream fis = null;
            try {
                    fis = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
            }
            ObjectInputStream ois = null;
            try {
                    ois = new ObjectInputStream(fis);
            } catch (IOException e) {

                    throw new RuntimeException(e);
            }
            //读取文件
            ois.readObject().toString();
            ois.close();
            } else if (choice == 2) {
            someMethod(2);
                // 从键盘输入信息
                System.out.println("请录入书籍编号");
                int id = sc.nextInt();
                System.out.println("请录入书籍名字");
                String name = sc.next();
                System.out.println("请录入书籍作者");
                String autor = sc.next();
                // 每上新一本书籍，就要创建一本书籍对象，初始化
                Book b = new Book(id, name, autor);
                list2.add(b);
                //流要以追加方式建立这个流加个参数
                System.out.println("书籍上架成功");
            return b;
        } else if (choice == 3) {
            someMethod(3);
            Book c=null;
                // 录入想要删除的图书编号
                System.out.println("请输入您想要下架的书籍编号");
                int delNo = sc.nextInt();
                for (int i = 0; i < list2.size(); i++) {
                    Book b = (Book) list2.get(i);
                    if (b.getId() == delNo) {
                        list2.remove(b);
                        c=b;
                        System.out.println("书记下架成功");
                        break;
                    }
                }
                translist();
                return c;
            } else if (choice == 4) {
            someMethod(4);
            }
        return null;
    }
}